<?php

namespace app\admin\model\zhongtai;

use think\Model;

class UserWithdraw extends Model
{
// 表名
    protected $table = 'user_withdraw';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "user_id" => $v['user_id'],
                    "withdraw_id" => $v['id'],
                    "username" => $v['username'],
                    "phone" => $v['phone'],
                    "type" => $v['type'],
                    "money" => $v['money'],
                    "service_money" => $v['service_money'],
                    "pay_money" => $v['pay_money'],
                    "order_sn" => $v['order_sn'],
                    "orderNo" => $v['orderNo'],
                    "payNo" => $v['payNo'],
                    "payType" => $v['payType'],
                    "payTime" => $v['payTime'],
                    "payMsg" => $v['payMsg'],
                    "auditStatus" => $v['auditStatus'],
                    "auditMsg" => $v['auditMsg'],
                    "auditUsername" => $v['auditUsername'],
                    "auditUserId" => $v['auditUserId'],
                    "auditTime" => $v['auditTime'],
                    "role" => $v['role'],
                    "reticulum" => $v['reticulum'],
                    "post" => $v['post'],
                    "memberId" => $v['memberId'],
                    "systemId" => $v['systemId'],
                    "memberName" => $v['memberName'],
                    "bankName" => $v['bankName'],
                    "cardNo" => $v['cardNo'],
                    "idCard" => $v['idCard'],
                    "pay_qnum" => $v['pay_qnum'],
                    "with_type" => $v['with_type'],
                    "pay_merchantNo" => $v['pay_merchantNo'],
                    "pay_productCode" => $v['pay_productCode'],
                    "pay_taskCode" => $v['pay_taskCode'],
                    "remark" => $v['remark'],
                    "createtime" => $v['createtime'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'withdraw_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'withdraw_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "user_withdraw表更新完成";
    }
}
